package com.eunion.fileserver.upload.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.eunion.fileserver.ftp.FtpHelper;
import com.eunion.fileserver.upload.FileException;
import com.eunion.fileserver.upload.FileUploadAbstract;
import com.eunion.fileserver.upload.FileUploadHelper;
import com.eunion.fileserver.upload.FileVo;
import com.eunion.fileserver.upload.IFileUpload;
import com.eunion.fileserver.upload.ImageCompressUtil;

/**
 * 文件上传ftp的实现.
 * @since 2017年2月17日
 */
public class FileUploadFtp extends FileUploadAbstract implements IFileUpload {

	@Override
	public void uploadFileInputStream(FileVo fv) {
		checkPath(fv);

		String destFilename = FileUploadHelper.getUUID() + fv.getType();
		String returnFilePath = fv.getPath() + destFilename;
		fv.setNameNew(destFilename);//回写
		InputStream compresInputStream = null;
		try {
			FtpHelper ftp = FileUploadHelper.getInstance().getFtpHelper();
			//inpustream 不能2次使用。这里要提取处理转
			byte[] fileByte = ImageCompressUtil.readBytes(fv.getFileInputStream());
			InputStream input1 = ImageCompressUtil.byte2Input(fileByte);
			fv.setFileInputStream(input1);
			LOG.info("文件上传开始 ：{}", fv.getNameOrg());
			boolean flag = ftp.uploadFTPFileFromInputStream(fv.getFileInputStream(), destFilename, fv.getPath());
			LOG.info("文件上传:{} 为:{} ", flag, fv.getPath());
			if (!flag) {
				return;
			}
			//再判断是否要写个缩略版本。fullPath
			if (FileUploadHelper.getInstance().getImageCompress()) {
				if (fv.getType().toLowerCase().equals(".jpg") || fv.getType().toLowerCase().equals(".jpeg") || fv.getType().toLowerCase().equals(".png")) {
					String subfix = destFilename.substring(destFilename.lastIndexOf(".") + 1, destFilename.length());
					String newName = ImageCompressUtil.getCompressFileNamePath(destFilename);
					compresInputStream = ImageCompressUtil.compressAutoWidthHeight(fileByte, subfix);
					boolean flagCompress = ftp.uploadFTPFileFromInputStream(compresInputStream, newName, fv.getPath());
					LOG.info("文件，现在压缩:{} 为:{} ", flagCompress, newName);
				}
			}
			fv.setUpload(true);
			fv.setRelativePath(returnFilePath);
		} catch (Exception e) {
			throw new FileException("保存文件出错", e);
		} finally {
			try {
				if (fv != null && fv.getFileInputStream() != null) {
					fv.getFileInputStream().close();
				}
			} catch (IOException e) {
				LOG.error("关闭原inputstream失败", e);
			}
			try {
				if (null != compresInputStream) {
					compresInputStream.close();
				}
			} catch (IOException e) {
				LOG.error("关闭压缩后inputstream失败", e);
			}
		}
	}

	@Override
	public boolean deleteFile(String deleteFile) {
		return FileUploadHelper.getInstance().getFtpHelper().deleteFTPFile(deleteFile);
	}

	@Override
	public boolean downloadFile(String source, String destination) {
		return FileUploadHelper.getInstance().getFtpHelper().downloadFTPFile(source, destination);
	}

	@Override
	public InputStream getFileInputStream(String source) {
		return FileUploadHelper.getInstance().getFtpHelper().downloadFTPFile(source);
	}

	@Override
	public List<String> listFiles(String directory) {
		return FileUploadHelper.getInstance().getFtpHelper().listFTPFiles(directory);
	}

}
